package cn.fl.mcr.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.transform.McrCommCstRecordVOTransform;
import cn.fl.mcr.transform.McrHtkOrderInfoVOTransform;
import cn.fl.mcr.vo.McrCommCstRecordVO;
import cn.fl.system.constant.SysLogConstant;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrHtkOrderInfoQC;
import cn.lionbridgecapital.loan.model.constant.McrCommConstant;
import cn.lionbridgecapital.loan.model.dto.McrCommCstRecordDTO;
import cn.lionbridgecapital.loan.model.dto.McrHtkOrderInfoDTO;
import cn.lionbridgecapital.loan.model.vo.McrHtkOrderInfoVO;
import cn.lionbridgecapital.loan.service.api.IMcrHtkOrderInfoApiService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : wsw
 * @date : 2019/12/06
 * @desc :
 */
@Controller
@RequestMapping("/mcr/mcrHtkOrder")
public class McrHtkOrderController {

    private static final Logger logger = LoggerFactory.getLogger(McrCstCrLimSchController.class);

    @Autowired
    private IMcrHtkOrderInfoApiService mcrHtkOrderInfoApiService;


    /**
     * 日期参数绑定
     * @param binder
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        CustomDateEditor customDateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, customDateEditor);
    }


    /**
     * 到列表页面
     */
    @RequestMapping("/toListPage.do")
    public ModelAndView toListPage() {
        ModelAndView view = new ModelAndView("/mcr/mcrHtkOrder/mcrHtkOrderList");
        BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 到添加页面
     * 目前只添加外部白名单
     */
    @RequestMapping("/toAddPage.do")
    public ModelAndView toAddPage(Long id) {
        ModelAndView view = new ModelAndView("/mcr/mcrHtkOrder/mcrHtkOrderAdd");
        BaseLogUtils.info(logger, "toAddPage", "跳转到新增页面, [id="+id+"]", CurrentThreadContext.getCurrentUserName());
        view.addObject("id", id);
        return view;
    }

    /**
     * 到修改页面
     *
     */
    @RequestMapping("/toModifyPage.do")
    public ModelAndView toModifyLgsPage(Long id) {
        ModelAndView view = new ModelAndView("/mcr/mcrHtkOrder/mcrHtkOrderModify");
        BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面, [id="+id+"]", CurrentThreadContext.getCurrentUserName());
        view.addObject("id", id);
        return view;
    }

    /**
     * 列表数据
     * @return
     */
    @RequestMapping("/initList.do")
    public @ResponseBody ListPageVO initList(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10")Integer pagesize,
                                             McrHtkOrderInfoQC qc) {
        // 查询
        Pager queryPager = new Pager();
        queryPager.setCurrentPage(page);
        queryPager.setEveryPage(pagesize);
        queryPager.setCondition(qc);
        BaseLogUtils.info(logger, "initList", "获取列表, queryPager:" + JSONObject.toJSONString(queryPager),
                CurrentThreadContext.getCurrentUserName());

        // 初始化返回数据
        Integer totalCount = 0;
        List<McrHtkOrderInfoVO> resultList = new ArrayList();
        try {
            Pager resultPager = mcrHtkOrderInfoApiService.searchMcrHtkOrderListPage(queryPager);
            BaseLogUtils.info(logger, "initList", "获取列表, resultPager:" + JSONObject.toJSONString(resultPager),
                    CurrentThreadContext.getCurrentUserName());
            // 处理返回数据
            if(resultPager != null && CollectionUtils.isNotEmpty(resultPager.getResultList())){
                totalCount = resultPager.getTotalCount();
                resultList = McrHtkOrderInfoVOTransform.toVOList((List< McrHtkOrderInfoDTO>)resultPager.getResultList());
            }
        } catch (Exception e) {
            BaseLogUtils.error(logger, "add", "获取列表异常, queryPager:" + JSONObject.toJSONString(queryPager),
                    CurrentThreadContext.getCurrentUserName(), e);
        }

        // 返回数据
        ListPageVO<McrHtkOrderInfoVO> listPageVO = new ListPageVO<McrHtkOrderInfoVO>(resultList, totalCount);
        BaseLogUtils.info(logger, "initList", "获取列表, listPageVO:" + JSONObject.toJSONString(listPageVO),CurrentThreadContext.getCurrentUserName());
        return listPageVO;
    }


    /**
     * 新增数据
     * @return
     */
    @RequestMapping("/add.do")
    public @ResponseBody DataResultVO add(McrCommCstRecordVO vo) {

        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);

        BaseLogUtils.info(logger, "add", "添加数据, vo:" + JSONObject.toJSONString(vo),
                CurrentThreadContext.getCurrentUserName());

        McrCommCstRecordDTO dto = McrCommCstRecordVOTransform.toDTO(vo);
        dto.setCrtTm(new Date());
        dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        dto.setBusType(McrCommConstant.MCR_LOAN_TYPE_OUT);
        BaseLogUtils.info(logger, "add", "添加数据, dto:" + JSONObject.toJSONString(dto),
                CurrentThreadContext.getCurrentUserName());

        try {
//            mcrHtkOrderInfoApiService.addMcrCommCstRecord(dto);
        } catch (Exception e) {
            BaseLogUtils.error(logger, "add", "添加数据异常, dto:" + JSONObject.toJSONString(dto),
                    CurrentThreadContext.getCurrentUserName(), e);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("添加数据失败！");
            return resultVO;
        }

        BaseLogUtils.info(logger, "add", "添加数据, resultVO:" + JSONObject.toJSONString(resultVO),
                CurrentThreadContext.getCurrentUserName());

        return resultVO;
    }

    /**
     * 修改数据
     * @return
     */
    @RequestMapping("/modify.do")
    public @ResponseBody DataResultVO modify(McrCommCstRecordVO vo) {

        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);

        BaseLogUtils.info(logger, "modify", "修改数据, vo:" + JSONObject.toJSONString(vo),
                CurrentThreadContext.getCurrentUserName());

        McrCommCstRecordDTO dto = null;
        if(vo.getBusType().equals(McrCommConstant.MCR_LOAN_TYPE_OUT)){
            dto = McrCommCstRecordVOTransform.toDTO(vo);
        }else if (vo.getBusType().equals(McrCommConstant.MCR_LOAN_TYPE_LOGS)){
            dto = new McrCommCstRecordDTO();
            dto.setId(vo.getId());
            dto.setMdfStp(vo.getMdfStp());
        }

        if(dto == null){
            BaseLogUtils.info(logger, "modify", "非外部和物流白名单不允许修改",
                    CurrentThreadContext.getCurrentUserName());
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("非外部和物流白名单不允许修改！");
            return resultVO;
        }

        dto.setMdfTm(new Date());
        dto.setMdfUsrId(CurrentThreadContext.getCurrentUserId());

        BaseLogUtils.info(logger, "modify", "修改数据, dto:" + JSONObject.toJSONString(dto),
                CurrentThreadContext.getCurrentUserName());

        try {
//            mcrHtkOrderInfoApiService.modifyMcrCommCstRecord(dto);
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "修改数据异常, dto:" + JSONObject.toJSONString(dto),
                    CurrentThreadContext.getCurrentUserName(), e);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("修改数据失败！");
            return resultVO;
        }

        BaseLogUtils.info(logger, "modify", "修改数据, resultVO:" + JSONObject.toJSONString(resultVO),
                CurrentThreadContext.getCurrentUserName());

        return resultVO;
    }

    /**
     * 删除功能，不搞
     * @return
     */
    @RequestMapping("/removeById.do")
    public @ResponseBody DataResultVO removeById() {
        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        return resultVO;
    }


    /**
     * 主键获取
     * @return
     */
    @RequestMapping("/getById.do")
    public @ResponseBody DataResultVO getById(Long id) {

        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);

        BaseLogUtils.info(logger, "getById", "查询数据, id:" + id, CurrentThreadContext.getCurrentUserName());

        McrCommCstRecordDTO dto = null;
        try {
//            dto = mcrHtkOrderInfoApiService.selectMcrCommCstRecordById(id);
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getById", "查询数据, id:" + id,
                    CurrentThreadContext.getCurrentUserName(), e);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("查询数据失败！");
        }
        if(dto == null){
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("不存在此条数据！");
        }

        resultVO.setData(McrCommCstRecordVOTransform.toVO(dto));
        BaseLogUtils.info(logger, "getById", "查询数据, resultVO:" + JSONObject.toJSONString(resultVO),
                CurrentThreadContext.getCurrentUserName());

        return resultVO;
    }


}
